<template>
    <div>
        <mian-layout @refresh="resetForm('search_data')">
            <template v-slot:conTop>
                <el-form size="mini" :inline="true" :model='search' ref="search_data" class="demo-form-inline search-form">
                    <el-form-item label="商品分类名称" prop="name">
                        <el-input v-model="search.name" placeholder="输入商品分类名称"></el-input>
                    </el-form-item>
                    <el-form-item label="创建时间" prop="dataTime">
                        <el-date-picker v-model="dataTime" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" value-format="yyyy-MM-dd HH:mm:ss" />
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" plain  icon="el-icon-search" @click="doSearch">搜索</el-button>
                        <el-button icon="el-icon-refresh-right" @click="resetForm('search_data')">重置</el-button>
                    </el-form-item>
                </el-form>   
            </template>
            <div class="btnBox" style="padding-bottom:10px">
                <el-button type="success" icon="el-icon-plus" size="mini" @click="opegDg">创建分类</el-button>
                <!-- <el-button type="danger" @click='moreDel' icon="el-icon-close" size="mini" plain>批量删除</el-button> -->
            </div>
            <my-table :loading="loading" :lists="tableData" @selectionChange="tableSelect" @pageChange="handleChange" :sequence="true" :page='page'>
                
                <el-table-column property="name" label="商品分类名称" align='center'></el-table-column>
                <el-table-column property="goodsNum" label="关联商品数量" align='center'></el-table-column>
                <el-table-column property="timestamp" label="创建时间" align='center'>
                    <template slot-scope="scope">
                        <span>{{scope.row.createTime}}</span>
                    </template>
                </el-table-column>
                
                
                <el-table-column prop="operation" align='center' label="操作" width="200">
                    <template slot-scope='scope'>
                        <el-button size="mini" type="success" plain icon="el-icon-edit" @click="edit(scope.row)">修改</el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete" @click="del(scope.row)">删除</el-button>
                    </template>
                </el-table-column>

            </my-table>
        </mian-layout>
        <my-dialog v-if="dialogVisible" :show.sync="dialogVisible" :showBtn="false" width="500px" title="创建商品分类">
            <el-form size="small" :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="商品分类名称" prop="name">
                    <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>  
            </el-form> 
            <div style="text-align: right">
                <el-button  size="small" @click="dialogVisible=false" icon="el-icon-close">取 消</el-button>
                <el-button type="primary" size="small" icon="el-icon-check" @click="save">确 定</el-button>
            </div>
        </my-dialog> 
    </div>
</template>
<script>
import {mapGetters} from "vuex"
import myTable from '@/components/public/myTable';
import myDialog from '@/components/public/myDialog';
import {
  get_cateory_list,
  add_cateory,
  del_cateory,
  edit_cateory
} from '@/api/arApi'
export default {
    components:{
      myTable,
      myDialog
    },
    data(){
        return{
            search:{
                name:'',
                startTime: "",
                endTime: "",
                orderField: "",
                orderType: "",
            },
            page:{
                pageSize:10,
                pageIndex:1,
			    total:0,
            },
            loading:true,
            tableData:[],
            multipleSelectionIds:[],
            dataTime:'',
            dialogVisible:false,
            ruleForm:{
                name:''
            }
        }
    },
    computed:{
        ...mapGetters({
            'userInfo':'user/userInfo'
        })
    },
    created(){
        this.getList()
    },
    methods:{
        doSearch(){
            this.pageIndex = 1
            this.getList()
        },
        getList(){ 
            this.loading = true
            if(this.dataTime){
                this.search.startTime = this.dataTime[0]
                this.search.endTime = this.dataTime[1]
            }
            get_cateory_list({
                limit: this.page.pageSize,
                page: this.page.pageIndex,
                ...this.search
            }).then(res=>{
                this.loading = false
                this.tableData = res.data
                this.page.total = res.count
            })
        },
        
        tableSelect(item){
            this.multipleSelectionIds = []
            for(let i=0;i<item.length;i++){
                this.multipleSelectionIds.push(item[i]._id)
            }
        },
        handleChange(index) {
            this.page.pageIndex = index;
            this.getList();
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dataTime = []
            for (const key in this.search) {
               this.search[key] = '' 
            }
            this.page.pageIndex = 1
            this.getList()
        },
        opegDg(){
            this.ruleForm.goodsCategoryId = ''
            this.ruleForm.name = ''
            this.dialogVisible=true
        },
        async save(item){
            if(!this.ruleForm.name) return this.$message.warning('请输入商品分类名称');
            let fn = add_cateory
            if(this.ruleForm.goodsCategoryId){
                fn = edit_cateory
            }
            const res = await fn({...this.ruleForm})
            if(res.success){
                this.$message.success('添加成功')
                this.page.pageIndex = 1
                this.getList()
                this.dialogVisible = false
            }else{
                this.$message.error('添加失败');
            }
        },
        del(item) {
            if(item.goodsNum>0) return this.$message.error('该类别下有关联商品，不能删除！');
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const res = await del_cateory({goodsCategoryId:item.goodsCategoryId})
                if(res.success){
                    this.$message.success('删除成功')
                    this.page.pageIndex = 1
                    this.getList()
                }else{
                    this.$message.error('删除失败');
                }
            }).catch(() => {});
        },
        edit(item){
            this.ruleForm.name = item.name
            this.ruleForm.goodsCategoryId = item.goodsCategoryId
            this.dialogVisible = true
        }
    }
}
</script>